import store from '@/store'
import { Message } from 'element-ui'
import { copyStructure } from '@/api/fmea/common/structureTree'
const nodeTypeList = store.state.fmea.nodeType

// 粘贴节点
export function handlePasteNode(source, target, projectId, step, callback) {
  const sourceMainNodeData = source.getMainNode().data
  const targetMainNodeData = target.getMainNode().data
  const targetStructureId = targetMainNodeData.extraId
  const sourceStrucNodetype = sourceMainNodeData.nodeType
  const targetStrucNodetype = targetMainNodeData.nodeType

  if (targetStrucNodetype === nodeTypeList.product.value) {
    messTip()
    return
  }

  if (sourceStrucNodetype < targetStrucNodetype) {
    messTip()
    return
  }

  if (source.data.nodeType <= target.data.nodeType && source.data.nodeType !== nodeTypeList.bom.value) {
    messTip()
    return
  }

  if (source.data.nodeType > sourceStrucNodetype && sourceStrucNodetype !== targetStrucNodetype) {
    messTip()
    return
  }

  if (source.data.nodeType === nodeTypeList.bomOut.value || source.data.nodeType === nodeTypeList.interfaceInside.value || source.data.nodeType === nodeTypeList.interfaceOutside.value) {
    messTip()
    return
  }

  if (source.data.nodeType === nodeTypeList.measurePrevent.value) {
    if (target.data.nodeType !== nodeTypeList.failure.value && target.data.nodeType !== nodeTypeList.failureReason.value) {
      messTip()
      return
    }
  }

  if (source.data.nodeType === nodeTypeList.measureDetection.value) {
    if (target.data.nodeType !== nodeTypeList.failureReason.value && target.data.nodeType !== nodeTypeList.failureMode.value && target.data.nodeType !== nodeTypeList.failure.value) {
      messTip()
      return
    }
  }

  if (source.data.nodeType >= nodeTypeList.failure.value && source.data.nodeType <= nodeTypeList.failureReason.value) {
    if (target.data.nodeType !== nodeTypeList.character.value && target.data.nodeType !== nodeTypeList.function.value) {
      messTip()
      return
    }
  }

  if (source.data.nodeType === nodeTypeList.character.value || source.data.nodeType === nodeTypeList.function.value) {
    if (source.data.nodeType < target.data.nodeType) {
      messTip()
      return
    }
  }

  const params = {
    step,
    sourceId: source.data.extraId,
    sourceNodeType: source.data.nodeType,
    targetId: target.data.extraId,
    targetProjectId: projectId,
    targetStructureId: targetStructureId
  }
  copyStructure(params).then(res => {
    callback(res)
  })
}

function messTip() {
  Message.error('无效操作！')
}
